<?php
// +----------------------------------------------------------------------
// | motto: 不懈努力，只为遇见更好的自己
// +----------------------------------------------------------------------
// | Copyright: (c) 2020 http://www.slong.ink All rights reserved.
// +----------------------------------------------------------------------
// | Licensed: http://www.apache.org/licenses/LICENSE-2.0
// +----------------------------------------------------------------------
// | Author: slongzhang <slongzhang@qq.com>
// +----------------------------------------------------------------------
// | Date: 2023/11/4
// +----------------------------------------------------------------------
// | Time: 22:19
// +----------------------------------------------------------------------

namespace Extend\facade;

use GuzzleHttp\Client;

class HttpClient
{
    private static $facadeClientList = [];
    private static $facadeCookieList = [];

    // 初始化实例
    private static function facadeInitInstance($options, $name = '') {
        if (empty($name) || !is_string($name)) {
            $name = md5(json_encode($options, 320));
        }
        if (!empty($options['cookies'])) {
            if ($options['cookies'] === 'default') {
                static::$facadeCookieList[$name] = new \GuzzleHttp\Cookie\CookieJar;
                $options['cookies'] = &static::$facadeCookieList[$name];
            }
            else {
                static::$facadeCookieList[$name] = &$options['cookies'];
            }
        }
        static::$facadeClientList[$name] = new Client($options);
        return static::$facadeClientList[$name];
    }

    // 获取实例
    public static function facadeGetClient(...$args)
    {
        if (empty($args)) {
            $name = 'default';
        }
        else {
            $argsLen = count($args);
            if ($argsLen == 1) {
                if (is_array($args[0])) {
                    $options = $args[0];
                    $name = md5(json_encode($options, 320));
                }
                elseif (is_string($args[0])) {
                    $name = $args[0];
                }
                else {
                    $name = 'default';
                }
            }
            else {
                $options = $args[0];
                $name = (empty($args[1]) || !is_string($args[1]))? 'default': $args[1];
            }
        }

        if (empty(static::$facadeClientList[$name])) {
            if (!isset($options) || !is_array($options)) {
                // 进行实例化
                $options = [
                    // Base URI is used with relative requests
                    // 'base_uri' => 'http://httpbin.org',
                    // You can set any number of default request options.
                    'timeout'  => 60,
                    'cookies'  => 'default',
                    'verify'   => false,
                    'headers'   => [
                        'Accept' => 'application/json',
                        'Accept-Encoding' => 'gzip, deflate, br',
                        'Accept-Language' => 'zh-CN,zh;q=0.9',
                        'Content-Type' => 'application/x-www-form-urlencoded',
                        'Sec-Ch-Ua' => '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
                        'Sec-Ch-Ua-Mobile' => '?0',
                        'Sec-Ch-Ua-Platform' => '"Windows"',
                        'Sec-Fetch-Dest' => 'empty',
                        'Sec-Fetch-Mode' => 'cors',
                        'Sec-Fetch-Site' => 'cross-site',
                        'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
                    ]
                ];
            }
            static::facadeInitInstance($options, $name);
        }
        return static::$facadeClientList[$name];
    }


    // 手动初始化
    public static function facadeInit($options = [], $name = '') {
        if (empty($name) || !is_string($name)) {
            $name = 'default';
        }
        return static::facadeInitInstance($options, $name);
    }


    // 获取指定实例的cookie
    public static function facadeGetCookie($name = '') {
        if (empty($name) || !is_string($name)) {
            $name = 'default';
        }
        return static::$facadeCookieList[$name]? static::$facadeCookieList[$name]: null;
    }

    // 魔术方法
    public static function __callStatic($method, $arguments)
    {
        $client = static::facadeGetClient();

        return call_user_func_array([$client, $method], $arguments);
    }
}
